package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("adminOrderController")
@RequestMapping("/admin/order/")
public class OrderController {

    @Autowired
    OrderService orderService;



    @GetMapping("/conditionSearch")
    public Result conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("订单搜索 :{}",ordersPageQueryDTO);
        PageResult result = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(result);
    }

    @GetMapping("/statistics")
    public Result statistics(){
        log.info("各个状态的订单数量统计 ");
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }

    @PutMapping("/confirm")
    public Result confirm(@RequestBody Orders orders){
        log.info("接单:{}",orders);
        orders.setStatus(3);
        orderService.update(orders);
        return Result.success();
    }

    @PutMapping("/rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO){
        log.info("拒单 :{}",ordersRejectionDTO);
        Orders orders = Orders
                .builder()
                .id(ordersRejectionDTO.getId())
                .status(6)
                .rejectionReason(ordersRejectionDTO.getRejectionReason())
                .build();
        orderService.update(orders);
        return Result.success();
    }

    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO){
        log.info("取消订单  :{}",ordersCancelDTO);
        Orders orders = Orders
                .builder()
                .id(ordersCancelDTO.getId())
                .status(6)
                .cancelReason(ordersCancelDTO.getCancelReason())
                .build();
        orderService.update(orders);
        return Result.success();
    }

    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable Long id){
        log.info("派送订单 :{}",id);
        Orders orders = Orders
                .builder()
                .id(id)
                .status(4)
                .build();
        orderService.update(orders);
        return Result.success();
    }

    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Long id){
        log.info("完成订单:{}",id);
        Orders orders = Orders
                .builder()
                .id(id)
                .status(5)
                .build();
        orderService.update(orders);
        return Result.success();
    }
}
